package com.heiyu.utils;

import java.util.HashMap;
import java.util.Map;

import com.heiyu.dao.BaseMapper;

public class PageHelper {

    public static Map<String,Object> page(BaseMapper mapper,Map<String,Object> param,Integer index,Integer size){
        Map<String,Object> result = new HashMap<String,Object>();
        Integer count = mapper.count(param);
        if(count == null || count ==0){
            return result;
        }
        if(count/size<index){
            index = count/size;
            if(count%size>0){
                index +=1;
            }
        }
        result.put("total",count);
        result.put("pageIndex",index);
        index = (index-1)*size;
        param.put("index",index);
        param.put("offset",size);
        param.put("sort","desc");
        result.put("list",mapper.page(param));
        return result;
    }

    public static Map<String,Object> page(BaseMapper mapper,Map<String,Object> param){
        Integer index = 1;
        Integer size = 20;
        if(param.containsKey("index")){
            index = Integer.valueOf(param.get("index").toString());
            param.remove("index");
        }
        if(param.containsKey("size")){
            size = Integer.valueOf(param.get("size").toString());
            param.remove("size");
        }
        return page(mapper,param,index,size);
    }

}
